<?php


namespace App\Http\observer;


use App\Exceptions\ApiException;
use App\Models\Floor;
use App\Models\Property;

class PropertyObserver
{
    public function creating(Property $property)
    {
        $count=Property::where("name",$property->name)->count();
        if ($count){
            throw new ApiException("名称重复");
        }
    }

    public function updating(Property $property)
    {
        $data=Property::where("name",$property->name)->first();
        if ($data){
            if ($data["id"]!=$property->id)
                throw new ApiException("名称重复");
        }
    }

    public function deleting(Property $property)
    {
        $floor=Floor::where("pid",$property->id)->first();
        if ($floor)
            throw new ApiException("楼盘中还有楼层，不能删除");
    }
}
